package hci.utils;

import java.awt.Color;
import java.awt.Polygon;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlDataHandler extends DefaultHandler {

	private boolean in_document;
	private String filename;
	private String image;
	private boolean in_polygon;
	private boolean in_color;
	private boolean in_red;
	private boolean in_green;
	private boolean in_blue;
	private boolean in_label;
	private boolean in_point;
	private boolean in_y;
	private boolean in_x;
	public ArrayList<MyPolygon> myPolygons;
	private MyPolygon currentPolygon;
	private Point currentPoint;
	private int currentRed, currentGreen, currentBlue;
	public String imageFile;

	public void startDocument() throws SAXException {

		myPolygons = new ArrayList<MyPolygon>();
	}

	public void endDocument() throws SAXException {

	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		if (qName.equals("document")) {

			in_document = true;
			filename = attributes.getValue("filename");
			imageFile = attributes.getValue("image");
			System.out.println("XDH image file: " + imageFile);
		}

		if (qName.equals("polygon")) {

			in_polygon = true;
			currentPolygon = new MyPolygon();
			currentPolygon.setVertexs(new ArrayList<Point>());
		}

		if (qName.equals("color")) {

			in_color = true;
		}

		if (qName.equals("red")) {

			in_red = true;
		}
		
		if (qName.equals("green")) {

			in_green = true;
		}
		
		if (qName.equals("blue")) {

			in_blue = true;
		}
		
		if (qName.equals("label")) {

			in_label = true;
		}
		
		if (qName.equals("point")) {

			in_point = true;
			currentPoint = new Point();
		}
		
		if (qName.equals("x")) {
			
			in_x = true;
		}
		
		if (qName.equals("y")) {

			in_y = true;
		}
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		String text = new String(ch,start,length);
		
		if (in_point) {
			
			if (in_x) {
				
				currentPoint.setX(Integer.valueOf(text));
			}
			
			if (in_y) {
				
				currentPoint.setY(Integer.valueOf(text));
			}
		}
		
		if (in_label) {
			
			currentPolygon.setLabel(text);
		}
		
		if (in_color) {
			
			if (in_red) {
				
				currentRed = Integer.valueOf(text);
			}
			
			if (in_green) {
				
				currentGreen = Integer.valueOf(text);
			}
			
			if (in_blue) {
				
				currentBlue = Integer.valueOf(text);
			}
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if (qName.equals("point")) {
			
			currentPolygon.getVertexs().add(currentPoint);
			in_point = false;
		}
		
		if (qName.equals("label")) {
			
			in_label = false;
		}
		
		if (qName.equals("polygon")) {
		
			int px[] = new int[currentPolygon.getVertexs().size()];
			int py[] = new int[currentPolygon.getVertexs().size()];
			int i = 0;
			
			for (Point p : currentPolygon.getVertexs()) {
				
				px[i] = p.getX();
				py[i] = p.getY();
			}
			Polygon p = new Polygon(px,py,currentPolygon.getVertexs().size());
			currentPolygon.setPolygon(p);
			myPolygons.add(currentPolygon);
			in_polygon = false;
		}
		
		if (qName.equals("color")) {
			
			currentPolygon.setColor(new Color(currentRed, currentGreen, currentBlue));
			in_color = false;
		}
		
		if (qName.equals("red")) {
			
			in_red = false;
		}
		
		if (qName.equals("green")) {
			
			in_green = false;
		}
		
		if (qName.equals("blue")) {
			
			in_blue = false;
		}
		
		if (qName.equals("x")) {
			
			in_x = false;
		}
		
		if (qName.equals("y")) {
			
			in_y = false;
		}
	}

}
